package com.recipes.meg;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Edit2 extends Activity {
	// Declare variables to hold sqlite database and database helper
	SQLiteDatabase db;
	DatabaseHelper db_helper;
	
	// Declare variable to hold id of recipe
	int id;
	
	// Declare EditText boxes to hold instructions and description
	EditText instructions_in;
	EditText description_in;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit2);
        
        // Get id of this recipe from previous activity
        id = this.getIntent().getExtras().getInt("id");
        
        // Instantiate new database helper variable
        db_helper = new DatabaseHelper(this);
        
        // Open database for reading
        db = db_helper.getWritableDatabase();
        
        // Get this recipes current instructions
        String cur_instructions = db_helper.getInstructions(db, id);
        
        // Get this recipes current description
        String cur_description = db_helper.getDescription(db, id);
        
        // Initialize save button
        Button save = (Button) findViewById(R.id.save);
         
        // Initialize EditText box for instructions
		instructions_in = (EditText) findViewById(R.id.instructions_in);

		// Set initial text of this text box to be current instructions
		instructions_in.setText(cur_instructions);
		
		// Initialize EditText box for description
		description_in = (EditText) findViewById(R.id.description_in);
		
		// Set the initial text of this text box to be current description
		description_in.setText(cur_description);
		
        // Set onClickListener for save button
        save.setOnClickListener(new View.OnClickListener() {
			// Override onClickFunction
			@Override
			public void onClick(View v) {		
				// Get contents of instructions_in
				String instructions = instructions_in.getText().toString();
				
				// Check if instructions are blank
				if(instructions.equalsIgnoreCase("")){
					// If it is then give an error message and return prematurely
					Toast.makeText(getApplicationContext(), "Recipe cannot have blank instructions", Toast.LENGTH_SHORT).show();
					return;
				}
		
				// Get contents out of description_in
				String description = description_in.getText().toString();
				
				// Open database for reading and writing
				db = db_helper.getWritableDatabase();
				
				// Call function to update description and instructions for this recipe and save return value
				int result = db_helper.create2(db, id, instructions, description);
			
				// Close the database
				db.close();
				
				// Check if result was -1
				if(result == -1){
					// If so give an error message and return prematurely
					Toast.makeText(getApplicationContext(), "Recipe was not updated", Toast.LENGTH_SHORT).show();
					return;
				}
				
				// Finish this activity
				finish();
			}
		});
    }
}
